$( document ).ready(function() {
	// init delete buttons of current symptoms
	initSymptomClearButtons();
	initDrugIssueClearButtons();

	// init calendar
	if ($("#date-picker").length > 0) {
		$("#date-picker").datepicker();
	}
	
	// init delete button
	if ($("#btn-delete").length > 0) {
		$("#btn-delete").on('click', function(e){
			e.preventDefault();
			$.Dialog({
				shadow: true,
				overlay: true,
				icon: '<span class="icon-remove"></span>',
				title: 'Delete Confirmation',
				width: 400,
				padding: 10,
				onShow: function(_dialog){
 					var content = _dialog.children('.content');
 					content.html('<div class="subheader-secondary" style="width:400px">Do you want to delete patient\'s all (prescription, appointment ...etc) information?</div><br/><div><a id="btn-delete" href="' + $("#btn-delete").attr('href') + '" class="button">Delete</a></div>');
				}
			});
		});
	}
	
	// add new symptom
	if ($("#add-new-symptom").length > 0) {
		$("#add-new-symptom").click(function(e) {
   			e.preventDefault();
			
			var symptomId = $("#new-symptom").val(); // get the value selected in the drop down
			var symptomName = "";
			if (symptomId != -1) {
				// selected symptom from drop down
				symptomName = $("#new-symptom option[value='" + symptomId + "']").text();
				appendPrescriptionSymptom(symptomId, symptomName);
				$("#new-symptom").val(-1);
			}
			
			
			if ($.trim($("#create-new-symptom").val()).length > 0) {
				//  typed new symptom
				symptomId = 0;
				symptomName = $.trim($("#create-new-symptom").val());
				appendPrescriptionSymptom(symptomId, symptomName);
				$("#create-new-symptom").val("");
			}
		
		});
	}

	// add new drug-issue
	 if ($("#add-new-drug-issue").length > 0) {
		  $("#add-new-drug-issue").click(function(e){
			   e.preventDefault();
			   var drugId = $("#new-drug-issue-drugId").val(); // get selected drugid
			   var drugName = $("#new-drug-issue-drugId option[value='" + drugId + "']").text();
			   var termsPerDay = $.trim($("#new-drug-issue-terms-per-day").val());
			   var noOfDays = $.trim($("#new-drug-issue-no-of-days").val());
			   var treatmentTimeId = $("#new-drug-issue-treatment-time").val(); // get selected treatment time id
			   var treatmentTimeName = $("#new-drug-issue-treatment-time option[value='" + treatmentTimeId + "']").text();
			   if (drugId != -1 && parseInt(termsPerDay) > 0 && parseInt(noOfDays) > 0 && treatmentTimeId != -1) {
					var html = getPrescriptionDrugIssueHtml(drugId, drugName, termsPerDay, noOfDays, treatmentTimeId, treatmentTimeName);
					$("#prescription-drug-issue-container").append(html);
					$("#new-drug-issue-drugId").val(-1);
					$("#new-drug-issue-terms-per-day").val("");
					$("#new-drug-issue-no-of-days").val("");
					$("#new-drug-issue-treatment-time").val(-1);
					initDrugIssueClearButtons();
			   }
		  });
	 }
	
	// terms per day inbox validation
	if ($(".terms-per-day").length > 0) {
		$(".terms-per-day").mask("99");
	}
	
	// no of days inbox validation
	if ($(".no-of-days").length > 0) {
		$(".no-of-days").mask("99");
	}
	if ($("#prev-prescription-details-opener").length > 0) {
		$("#prev-prescription-details-opener").click(function(e){
			e.preventDefault();
			$.Dialog({
				overlay: true,
				shadow: true,
				icon: '<span class="icon-info"></span>',
				title: 'Past Visit Information',
				content: '',
				width: 500,
				onShow: function(_dialog){
					var content = _dialog.children('.content');
					content.html("<div style='padding: 10px;'>" + $("#prev-prescription-details-container").html() + "</div>");
				}
			});
		});
	}
	
	// add symptom into the text boxes
	function appendPrescriptionSymptom(symptomId, symptomName) {
		var currentSymptomLength = $("#prescription-symptom-container .input-control").length;
		var html = getPrescriptionSymptomHtml((currentSymptomLength + 2), symptomId, symptomName);
		$("#prescription-symptom-container").append(html);
		
		// initialize symptom clear button of newly added element
		initSymptomClearButtons();
	}
	
	function getPrescriptionSymptomHtml(symptomIndex, symptomId, symptomName) {
		  var sb = [];
		  sb.push('<div data-role="input-control" class="input-control text">');
		  sb.push('<input type="hidden" name="guiPrescription.syptomList[' + symptomIndex + '].syptomId" value="' + symptomId + '"/>');
		  sb.push('<input type="text" name="guiPrescription.syptomList[' + symptomIndex + '].name" readonly="readonly" value="' + symptomName + '"/>');
		  sb.push('<button tabindex="-1" class="btn-symptom-clear btn-clear" type="button"></button>');
		  sb.push('</div>');
		  return sb.join("");
	}
	
	 function getPrescriptionDrugIssueHtml(drugId, drugName, termsPerDay, noOfDays, treatmentTimeId, treatmentTimeName) {
		var drugIssueIndex = $("#prescription-drug-issue-container .drug-issue-container").length + 1;
		var sb = [];
		sb.push(
		'<tr>' + 
			'<td>' + drugName + '</td>' +
			'<td>' + termsPerDay + '</td>' +
			'<td>' + noOfDays + '</td>' +
			'<td>' + treatmentTimeName + '</td>' +
			'<td>' +
	            '<input type="hidden" name="guiPrescription.drugIssueList[' + drugIssueIndex + '].drug.drugId" value="' + drugId + '"/>' +
	            '<input type="hidden" name="guiPrescription.drugIssueList[' + drugIssueIndex + '].termsPerDay" value="' + termsPerDay + '"/>' +
	            '<input type="hidden" name="guiPrescription.drugIssueList[' + drugIssueIndex + '].noOfDays" value="' + noOfDays + '"/>' +
	            '<input type="hidden" name="guiPrescription.drugIssueList[' + drugIssueIndex + '].treatmentTime" value="' + treatmentTimeId + '"/>' +
				'<label class="input-control checkbox">' +
	                '<input type="checkbox" name="guiPrescription.drugIssueList[' + drugIssueIndex + '].issue"/>' +
	                '<span class="check"></span>' +
	            '</label>' +
	            '<i class="icon-remove btn-drugissue-clear button on-right"></i>' +
            '</td>' +
		'</tr>'
		);
		return sb.join("");
	}

	function initSymptomClearButtons() {
		  $(".btn-symptom-clear").unbind("click").bind("click", function(e) {
			   $(this).closest(".input-control").removeClass("text");
			   $(this).closest(".input-control").find("input").remove();
			   $(this).closest(".input-control").find("button").remove();
		  });
 	}

	function initDrugIssueClearButtons() {
		  $(".btn-drugissue-clear").unbind("click").bind("click", function(e) {
			   $(this).closest(".drug-issue-container").remove();
		  });
 	}
});
